import org.w3c.dom.ls.LSOutput;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86183
 */
public class Test2 {
/*存在连续三个奇数的数组
给你一个整数数组 arr，请你判断数组中是否存在连续三个元素都是奇数的情况：如果存在，请返回 true ；否则，返回 false 。*/
    public static void main(String[] args) {
        int[] array = new int[]{1,2,34,3,4,5,7,23,12};
        boolean result = toFind(array);
        System.out.println(result);
    }
    public static boolean toFind(int[] array){
        int count = 0;
        for (int i = 0; i < array.length; i++) {
            if (array[i] % 2 != 0){
                count++;
                if(count == 3){
                    return true;
                }
            }else {
                count = 0;
            }

        }
        return false;
    }


/*
* 多数元素
给定一个大小为 n 的数组，找到其中的多数元素。多数元素是指在数组中出现次数 大于 ⌊ n/2 ⌋ 的元素。
你可以假设数组是非空的，并且给定的数组总是存在多数元素。*/
    public static void main17(String[] args) {
        //多数元素是指在数组中出现次数 大于 ⌊ n/2 ⌋ 的元素。,所以可以先排序，直接去数组中间的值
        int[] array =  new int[]{3,2,3};
        Arrays.sort(array);
        System.out.println(array[array.length/2]);
    }

/*
* 只出现一次的数字
给定一个非空整数数组，除了某个元素只出现一次以外，其余每个元素均出现两次。找出那个只出现了一次的元素。*/
    public static void main16(String[] args) {
        int[] array =  new int[]{4,1,2,1,2};
        System.out.println(search1(array));
    }
    public static int search1(int[] array){
        int n = 0;
        for (int i = 0; i < array.length; i++) {
            n ^= array[i]; //通过调试n的值依次为4、5、7、6、4，也可以理解成0^4^1^2^1^2=4
        }
        return n;
    }

    /*两数之和
给定一个整数数组 nums 和一个整数目标值 target，请你在该数组中找出 和为目标值 target 的那 两个 整数，并返回它们的数组下标。*/
    public static void main15(String[] args) {
        int[] num =  new int[]{1,2,3,4,5,6};
        int[] result = search(num ,7);
        System.out.println(Arrays.toString(result));
    }
    public static int[] search(int[] array ,int target){
        int[] ret = new int[2];
        for (int i = 0; i < array.length ; i++) {
            for (int j = i+1; j < array.length ; j++) {
                if(array[i]+array[j] == target){
                    ret[0] = i;
                    ret[1] = j;
                }
            }
        }
        return ret;
    }

/*奇数位于偶数之前
题目内容：调整数组顺序使得奇数位于偶数之前。调整之后，不关心大小顺序。
如数组：[1,2,3,4,5,6]
调整后可能是：[1, 5, 3, 4, 2, 6]*/
    public static void main14(String[] args) {
        int[] array = new int[]{1,2,3,4,5,6};
        System.out.println(Arrays.toString(array));
        int left = 0;
        int right = array.length-1;
        while (left <= right){
            int tmp = 0;
            if(array[left] %2 ==0){
                tmp = array[left];
                array[left] = array[right];
                array[right] = tmp;
                right--;
            }else {
                left++;
            }
        }
        System.out.println(Arrays.toString(array));
    }

    /*实现一个方法 transform, 以数组为参数, 循环将数组中的每个元素 乘以 2 , 并设置到对应的数组元素上. 例如 原数组为 {1, 2, 3}, 修改之后为 {2, 4, 6}*/
    public static void main13(String[] args) {
        int[] array = new int[]{1,2,3};
        transform(array);
        System.out.println(Arrays.toString(array));
    }
    public static void transform(int[]array){
        for (int i = 0; i < array.length; i++) {
            array[i] = array[i] * 2;
        }
    }
    /*给定一个二维数组，请你编写一个求和函数，计算出这个数组元素的和
输出描述：输出二维数组元素的和*/
    public static void main12(String[] args) {
        int[][]  arr = {{11,33,55},{22,44,66,88},{131,214,315,146},{928,827,726,625},{424,525}};
        int sum=add(arr);
        System.out.println(sum);
    }
    public static int add(int[][] array){
        int sum = 0;
        for (int row = 0; row < array.length; row++) {
            for (int col = 0; col < array[row].length; col++) {
                sum += array[row][col];
            }
        }
        return sum;
    }

    /*描述:输入6个整数，先将其输出然后再将数组倒转，再次输出数组
输入描述：用户随机输入的6个int类型变量
输出描述：先输出用户输入顺序的数组,再输出反转后的数组（输出格式为Arrays.toString(arr)）*/
    public static void main11(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int[] array = new int[6];
        for (int i = 0; i < array.length; i++) {
            array[i] = scanner.nextInt();
        }
        System.out.println(Arrays.toString(array));
        int left = 0;
        int right = array.length-1;
        int tmp = 0;
        while (left<right){
            tmp = array[left];
            array[left] = array[right];
            array[right] = tmp;
            left++;
            right--;
        }

        System.out.println(Arrays.toString(array));
    }


    /*将用户输入的六个数字填入数组并找出数组中最大值和最小值
输入描述：随机输入的6个整数
输出描述：输出数组中的最大值，最小值(最大值最小值之间用空格隔开。若有多个最大值或最小值，输出一次即可)*/

    public static void main10(String[] args) {
        int[] ary = new int[6];
        int max;
        int min;
        Scanner scanner = new Scanner(System.in);
        for (int i = 0; i <ary.length ; i++) {
            ary[i]=scanner.nextInt();
        }
        max = ary[0];
        min = ary[0];
        for (int i = 1; i < ary.length; i++) {
            if(max<ary[i]){
                max = ary[i];
            }
        }
        for (int i = 1; i < ary.length; i++) {
            if (min>ary[i]){
                min=ary[i];
            }
        }
        System.out.println(max+" "+min);
    }
    public static void main9(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int[] array = new int[6];
        for (int i = 0; i < array.length; i++) {
            array[i] = scanner.nextInt();
        }
        Arrays.sort(array);
        System.out.println(array[array.length-1]+" "+array[0]);
    }
    /*给定一个数组, 将里面的元素逆序排列.
思路:设定两个下标, 分别指向第一个元素和最后一个元素. 交换两个位置的元素.
然后让前一个下标自增, 后一个下标自建*/
    public static void main8(String[] args) {
        int[] arr = {1, 2, 3, 4};
        fun2(arr);
        System.out.println(Arrays.toString(arr));
    }
    public static void fun2(int[] array){
        int left = 0;
        int right = array.length-1;
        int tmp = 0;
        while(left<right){
            tmp = array[left];
            array[left] = array[right];
            array[right] = tmp;
            left++;
            right--;
        }
    }
    /*冒泡排序*/
    //方法二
    public static void main7(String[] args) {
        int[] array = new int[]{1,3,5,6,7,9,4};
        Arrays.sort(array);
        System.out.println(Arrays.toString(array));
    }
    //方法一：
    /*1. 将数组中相邻元素从前往后依次进行比较，如果前一个元素比后一个元素大，则交换，一趟下来后最大元素就在数组的末尾
      2. 依次循环，直到数组中所有的元素都排列好*/
    public static void main6(String[] args) {
        int[] array = new int[]{1,3,5,6,7,9,4};
        int[] arr = bubbleSort(array);
        System.out.println(Arrays.toString(arr));
    }
    public static int[] bubbleSort(int[] array){
        for (int i = 0; i < array.length; i++) {
            boolean flg = false;
            for (int j = 0; j < array.length-i; j++) {
                int tmp = 0;
                if (array[j] > array[j+1]){
                    tmp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = tmp;
                    flg = true;
                }
            }
            if (flg == false){//判断数组已经有序
                return array;
            }
        }
        return array;
    }
    /*二分查找*/
    //方法二
    public static void main5(String[] args) {
        int[] array = new int[]{1,3,5,6,7,9,4};
        Arrays.sort(array);
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.binarySearch(array, 7));
    }
    //方法一
    public static void main4(String[] args) {
        int[] array = new int[]{1,3,5,6,7,9,4};
        Arrays.sort(array);
        System.out.println(Arrays.toString(array));
        int num =binarySearch(array,7);
        System.out.println(num);

    }
    public static int binarySearch(int[] array,int key){
        int left = array[0];
        int right = array[array.length-1];
        while (left <= right){
            int mid = (left+right)/2;
            if(array[mid] > key){
                right = mid -1;
            }else if (array[mid] < key){
                left = mid + 1;
            }else {
                return mid;
            }
        }
        return 0;
    }
    public static void main3(String[] args) {
        int[][] arr = {{1, 2, 3},{4,5,6}};
        for (int row = 0; row < arr.length; row++) {
            for (int col = 0; col < arr[row].length; col++) {
                System.out.print( arr[row][col]+" ");
            }
            System.out.println();
        }
        int[][] array = {{1, 2, 3},{4,5,6}};
        String array2 = Arrays.toString(array);
        System.out.println(array);

        String array1 = Arrays.deepToString(array);
        System.out.println(array1);

    }
    public static void main2(String[] args) {
        /*int[] array = new int[]{1,2,3};
        fun(array);
        String arr = Arrays.toString(array);
        System.out.println(arr);*/
        int[] array = new int[]{1,2,3};
        fun1(array);
        String arr = Arrays.toString(array);
        System.out.println(arr);
    }
    public static void fun1(int[] array){
        array[0] = 100;
        array[1] = 200;
        array[2] = 300;
    }
    public static void  fun(int[] array1){
        array1 = new int[]{2,3,4};
    }
    public static void main1(String[] args) {
        /* int[] array = new int[10];
    double[] array1 = new double[10];*/

//        int[] array ;
//        array = new int[10];
//        double[] array1;
//        array1= new double[10];
       //int[] array = new int[]{10,20,30};

    /*int[] array = new int[]{10,20,30};
    double[] array1 = new double[]{1.1,2.2};
    String[] array2 = new String[]{"haha","hello"};*/

        //int array[] = new int[10];//不推荐

        /*int[] array = {10,20,30};
        double[] array1 = {1.1,2.2};
        String[] array2 = {"haha","hello"};
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(array1));
        System.out.println(Arrays.toString(array2));*/
        /*int[] array2 ;
        array2 =new int[]{1,2,3};
        System.out.println(Arrays.toString(array2));*/
        int[] array = new int[]{1,2,3,4,5,6};
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i]+" ");
        }
       /* System.out.print(array[0]+" ");
        System.out.print(array[1]+" ");
        System.out.print(array[2]+" ");
        System.out.print(array[3]+" ");
        System.out.print(array[4]+" ");
        System.out.print(array[5]);*/
        int[] array1 = new int[]{1,2,3,4,5,6};
        for (int x:array1) {
            System.out.print(x + " ");
        }
        int[] array2 = new int[]{1,2,3,4,5,6};
        String arr = Arrays.toString(array2);
        System.out.println(arr);

        int a = 10;
        System.out.println(a);
        int[] array3 = new int[]{1,2,3,4,5,6};
        System.out.println(array3);

        int[] array4 = new int[]{1,2,3,4,5};
        int[] array5 = new int[]{6,7,8,9,10};
        array4[0] = 100;
        array4[2] = 300;
        array5[0] = 600;
        array5[1] = 700;
        array5[2] = 800;
        array4 = array5;
        array4[3] = 900;
        array4[4] = 1000;
        for (int i = 0; i < array4.length; i++) {
            System.out.print(array4[i]+" ");
        }
        System.out.println();
        for (int i = 0; i < array5.length; i++) {
            System.out.print(array5[i]+" ");
        }

        int[] array6 = null;
        System.out.println(array6.length);


    }


}
